<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="common/head :: htmlhead">

<body>
<link rel="stylesheet" th:href="@{/static/plugins/sweetalert.css}">
<script th:src="@{/static/plugins/ueditor/ueditor.config.js}"></script>
<script th:src="@{/static/plugins/ueditor/ueditor.all.min.js}"></script>
<script th:src="@{/static/plugins/ueditor/lang/zh-cn/zh-cn.js}"></script>
<script th:src="@{/static/plugins/sweetalert.min.js}"></script>
<style>
    .layui-form-select dl {
        z-index: 9999999999999999999999;
    }

</style>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">内容添加</div>
                <div class="layui-card-body">
                    <form class="layui-form" id="form1" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文章标题</label>
                                <div class="layui-input-block">
                                    <input id="title" type="text" th:value="${content.title}" name="title" lay-verify="required"
                                           lay-reqtext="标题不能为空！"  class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">所属类别</label>
                                <div class="layui-input-block">
                                    <select th:field="${content.typeid}" th:value="${content.typeid}" id="type" name="type">
                                        <option value=""></option>
                                        <option th:selected="${content.typeid=='1'}" value="1">新闻动态</option>
                                        <option th:selected="${content.typeid=='2'}" value="2">活动展讯</option>
                                        <option th:selected="${content.typeid=='3'}" value="3">通知通告</option>
                                        <option th:selected="${content.typeid=='4'}" value="4">学术成果</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg12">
                                <label class="layui-form-label">文章内容</label>
                                <div class="layui-input-block">
                                    <div>
                                        <script id="editor" type="text/plain" style="width:100%;height:500px;"></script>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button th:if="${show} ne 'no'"  type="button" class="layui-btn  layui-btn-normal"
                                        onclick="changecontent('yfb')">发布
                                </button>
                                <button type="button" class="layui-btn" onclick="changecontent('dsh')">提交</button>
                                <button type="button" class="layui-btn layui-btn-primary" onclick="changecontent('cgx')">存为草稿
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" th:inline="javascript">

    layui.use('form', function () {
        var form = layui.form;
        form.render(null, 'component-form-element');
    });
    $(function () {
        var ue = UE.getEditor('editor');
        ue.addListener("ready", function () {
            //赋值
            ue.setContent([[${content.context}]])
        });
    });

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');

    function changecontent(status) {
        var context = UE.getEditor('editor').getContent();
        var title = document.getElementById("title").value;
        var type = document.getElementById("type").value;
        var datas = {};
        datas["context"] = context;
        datas["title"] = title;
        datas["typeid"] = type;
        datas["status"] = status;
        datas["id"] = [[${content.id}]];
        if (title === "") {
            layer.msg('文章标题不能为空！', {icon: 2});
            return false;
        }
        if (type === "") {
            layer.msg('请选择新闻类型！', {icon: 2});
            return false;
        }
        layer.confirm('提交？', function (index) {
            $.ajax({
                url: "/changeContent",
                async: false,
                type: "POST",
                contentType: "application/json",
                data: JSON.stringify(datas),
                success: function (data) {
                    if (data === "success") {
                        layer.open({
                            title: '信息'
                            , content: '操作成功！'
                            , end: function () {
                                history.go(-1);
                            }
                        });
                    }
                }
            });
        })
    }
</script>
</body>
</html>